package com.gray.wow.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gray.wow.biz.PhotoBiz;
import com.gray.wow.common.*;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Photo;
import com.gray.wow.domain.User;

@Controller
@RequestMapping("/photo")
public class PhotoController {

	@Resource
	private PhotoBiz photoBiz;

	@ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	// 图片管理
	@RequestMapping("/manage")
	public String managePhoto(HttpServletRequest request, Integer page) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			PageModel pm = photoBiz.getPhoto(Page.HALF_DEFAULT_PAGESIZE, page);
			request.setAttribute("pageModel", pm);
			return "admin/photo/photo";
		}
		return null;
	}

	@RequestMapping("/add")
	public String add(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			return "admin/photo/savePhoto";
		}
		return null;
	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			Photo photo = photoBiz.getPhotoById(id);
			if (photo != null) {
				request.setAttribute("photo", photo);
			}

			return "admin/photo/savePhoto";
		}
		return null;
	}

	// 上传图片
	@RequestMapping("/upload")
	public @ResponseBody
	Photo upload(HttpServletRequest request, MultipartFile photo) {
		String directory = "/resources/upload/";
		String relativePath = "";
		String realPath = request.getSession().getServletContext()
				.getRealPath(directory);
		try {
			if (photo != null && !photo.isEmpty()) {
				FileUtils.copyInputStreamToFile(photo.getInputStream(),new File(realPath + "/" + photo.getOriginalFilename()));
				relativePath = directory + photo.getOriginalFilename();
				Photo pic = new Photo();
				pic.setLocalurl(relativePath);
				return pic;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	public @ResponseBody
	String save(Photo photo) {
		if (photoBiz.savePhoto(photo))
			return new String("success");
		return new String("fail");
	}

	@RequestMapping("/delete")
	public String delPhoto(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if(photoBiz.delPhoto(ids))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
